package net.facelib.sdk;

import net.gdface.sdk.CodeInfo;
import net.gdface.utils.CoreDebugLog;
import net.gdface.utils.CoreDebugLog.OutputString;
import net.facelib.sdk.UserInfoManagement.User;

import static net.facelib.sdk.FaceCaptureLog.CAPTURER_LOGGER;

import net.facelib.live.LiveCodeInfo;
import net.facelib.live.LiveStatus;
import net.facelib.live.NativeStatus;

/**
 * {@link FaceCaptureCallback}接口log输出实例<br>
 * {@link CoreDebugLog}日志输出形式实现所有回调方法,应用层可以此为基类实例自己的回调对象
 * @author guyadong
 *
 */
public class FaceCaptureLogCallback implements FaceCaptureCallback{
	private boolean trace;
	private CoreDebugLog coreDebugLog;
	public FaceCaptureLogCallback() {
		this(false);
	}
	/**
	 * 构造方法
	 * @param trace 是否输出异常堆栈信息,为{@code null}时不输出日志
	 */
	public FaceCaptureLogCallback(Boolean trace) {
		this(trace, (OutputString)null);
	}
	/**
	 * 构造方法
	 * @param trace 是否输出异常堆栈信息,为{@code null}时不输出任何日志
	 * @param outputString 
	 */
	public FaceCaptureLogCallback(Boolean trace, OutputString outputString) {
		if(null != trace){
			this.trace = trace;
			if(outputString == null){
				this.coreDebugLog = CAPTURER_LOGGER;
				this.coreDebugLog.enableConsoleLog();
			}else{
				this.coreDebugLog = new CoreDebugLog();
				this.coreDebugLog.setOutput(outputString);
			}
		}else{
			this.trace = false;
			// 只创建对象,不指定 OutputString 实例,不会输出日志
			this.coreDebugLog = new CoreDebugLog();
		}
	}	
	public FaceCaptureLogCallback(Boolean trace,CoreDebugLog coreDebugLog) {
		if(null != trace){
			this.trace = trace;
			if(coreDebugLog == null){
				this.coreDebugLog = CAPTURER_LOGGER;
			}else{
				this.coreDebugLog = coreDebugLog;
				this.coreDebugLog.enableConsoleLog();
			}
		}else{
			this.trace = false;
			// 只创建对象,不指定 OutputString 实例,不会输出日志
			this.coreDebugLog = new CoreDebugLog();
		}
	}	
	@Override
	public void onLiveFaces(CaptureContext context, LiveCodeInfo[] codes){
		for(int i=0;i<codes.length;++i){
			coreDebugLog.log("pos={} liveStatus={},score",codes[i].getPos(),codes[i].getLiveStatus(),codes[i].getScope());
		}
	}
	@Override
	public void onLiveFaceError(CaptureContext context, LiveStatus liveStatus){
		NativeStatus nativeStatus = liveStatus.getNativeStatus();
		coreDebugLog.log("liveStatus = {},error {},message={}", 
				liveStatus,
				null == nativeStatus ? null : nativeStatus.getCode(),
				null == nativeStatus ? null : nativeStatus.getMessage());
	}
	@Override
	public void onLiveFaceError(CaptureContext context, Throwable e){
		coreDebugLog.log("Exception {},message={}", e.getClass().getName(),e.getMessage());
		coreDebugLog.logTrace(trace, e);
	}
	@Override
	public CodeInfo[] onFaceDetected(CaptureContext context, CodeInfo[] codes){
		for(CodeInfo c:codes){
			coreDebugLog.log("pos={}",c.getPos());
		}
		return codes;
	}
	 
	@Override
	public void onFaceFeature(CaptureContext context, CodeInfo[] codes){
		for(CodeInfo c:codes){
			coreDebugLog.log("pos={}",c.getPos());
		}
	}
	
	@Override
	public void onFeatureError(CaptureContext context, CodeInfo[] codes, Throwable e){
		coreDebugLog.log("Exception {},message={}", e.getClass().getName(),e.getMessage());
		coreDebugLog.logTrace(trace, e);
	}
	
	@Override
	public void onRecongizeOne(CaptureContext context, CodeInfo code, User user,double similarity){
		coreDebugLog.log("user={} similarity={}", user,similarity);
	}
	@Override
	public void onRecongizeN(CaptureContext context, CodeInfo[] codes, User[] users, double[] similarity){
		for(int i=0;i<users.length;++i){
			coreDebugLog.log("user={} similarity={}", users[i],similarity[i]);
		}
	}
	
	@Override
	public void onRecongizeError(CaptureContext context, CodeInfo[] codes, Throwable e){
		coreDebugLog.log("Exception {},message={}", e.getClass().getName(),e.getMessage());
		coreDebugLog.logTrace(trace, e);
	}
	@Override
	public void onNotFaceDetectedException(CaptureContext context){
		
	}
	
	@Override
	public void onUndeclaredCaptureException(CaptureContext context,Throwable e){
		coreDebugLog.log("Exception {},message={}", e.getClass().getName(),e.getMessage());
		coreDebugLog.logTrace(trace, e);
	}
	@Override
	public void onUndeclaredException(Throwable e){
		coreDebugLog.log("Exception {},message={}", e.getClass().getName(),e.getMessage());
		coreDebugLog.logTrace(trace, e);
	}

	@Override
	public void onCameraException(Throwable e){
		coreDebugLog.log("Exception {},message={}", e.getClass().getName(),e.getMessage());
		coreDebugLog.logTrace(trace, e);
	} 
	@Override
	public void onUserFaceRegistered(UserInfoManagement userInfoManagement){
		coreDebugLog.log("{} faces of users registered",userInfoManagement.getAllUsers().size());
	}
	/**
	 * 返回是否输出异常堆栈信息
	 * @return 是否输出异常堆栈信息
	 */
	public boolean isTrace() {
		return trace;
	}

	/**
	 * 设置是否输出异常堆栈信息
	 * @param trace
	 * @return 当前对象
	 */
	public FaceCaptureLogCallback setTrace(boolean trace) {
		this.trace = trace;
		return this;
	}
}